package com.pur.zsms.supplier.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.DeleteFileUtil;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.service.MaterComputeService;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.shopmanage.dao.IShopDao;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.supplier.dao.SupplierDao;
import com.pur.zsms.supplier.dao.SupplierOrderDao;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierDistributeMaterPo;
import com.pur.zsms.supplier.po.SupplierDistributePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.po.SupplierQualificationPo;
import com.pur.zsms.supplier.service.ISupplierDistributeService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.ShopSupplierVo;
import com.pur.zsms.supplier.vo.SupplierDistributeMateVo;
import com.pur.zsms.supplier.vo.SupplierDistributeVo;
import com.pur.zsms.supplier.vo.SupplierOrderCustVo;
import com.pur.zsms.supplier.vo.SupplierQualificationVo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;

@Service("supplierDistributeService")
public class SupplierDistributeServiceImpl implements ISupplierDistributeService {
	String msg = "";

	@Resource(name = "selectFoodDao")
	private ISelectFoodDao selectFoodDaoImpl;

	@Resource(name = "supplierOrderDao")
	private SupplierOrderDao supplierOrderDao;

	@Resource(name = "supplierDao")
	private SupplierDao supplierDao;

	@Resource(name = "userDao")
	private IUserDao userDaoImpl;

	@Resource(name = "shopDao")
	private IShopDao shopDaoImpl;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "materComputerService")
	private MaterComputeService materComputerServiceImpl;

	public MaterComputeService getMaterComputerServiceImpl() {
		return materComputerServiceImpl;
	}

	public void setMaterComputerServiceImpl(MaterComputeService materComputerServiceImpl) {
		this.materComputerServiceImpl = materComputerServiceImpl;
	}

	public ISelectFoodDao getSelectFoodDaoImpl() {
		return selectFoodDaoImpl;
	}

	public void setSelectFoodDaoImpl(ISelectFoodDao selectFoodDaoImpl) {
		this.selectFoodDaoImpl = selectFoodDaoImpl;
	}

	public SupplierOrderDao getSupplierOrderDao() {
		return supplierOrderDao;
	}

	public void setSupplierOrderDao(SupplierOrderDao supplierOrderDao) {
		this.supplierOrderDao = supplierOrderDao;
	}

	public SupplierDao getSupplierDao() {
		return supplierDao;
	}

	public void setSupplierDao(SupplierDao supplierDao) {
		this.supplierDao = supplierDao;
	}

	public IShopDao getShopDaoImpl() {
		return shopDaoImpl;
	}

	public void setShopDaoImpl(IShopDao shopDaoImpl) {
		this.shopDaoImpl = shopDaoImpl;
	}

	public IUserDao getUserDaoImpl() {
		return userDaoImpl;
	}

	public void setUserDaoImpl(IUserDao userDaoImpl) {
		this.userDaoImpl = userDaoImpl;
	}

	public IMaterDao getMaterDaoImpl() {
		return materDaoImpl;
	}

	public void setMaterDaoImpl(IMaterDao foodDaoImpl) {
		this.materDaoImpl = foodDaoImpl;
	}

	public PaginationSupport findDistributeMater(PageController pageController, SupplierDistributeMateVo supplierVo) throws GenericException {
		return supplierDao.findDistributeMater(pageController, supplierVo);
	}

	public PaginationSupport findDistributeMaterForGather(PageController pageController, SupplierDistributeMateVo supplierVo) throws GenericException {
		return supplierDao.findDistributeMaterForGather(pageController, supplierVo);
	}

	public int findDistributeMater(SupplierDistributeMateVo supplierVo) throws GenericException {
		return supplierDao.findDistributeMater(supplierVo);

	}

	public PaginationSupport findMaterId(PageController pageController, MaterVo materVo) throws GenericException {
		return materDaoImpl.findMaterId(pageController, materVo);
	}

	public String checkSupplierDistributeForGather(MaterVo materVo) throws GenericException {

		List<MaterPo> materList = materDaoImpl.getMaterPo(materVo);

		StringBuffer jsonMaterTestValue = new StringBuffer("");

		if (materList != null && materList.size() > 0) {

			jsonMaterTestValue.append("[");
			for (MaterPo materPo : materList) {
				jsonMaterTestValue.append("{\"");
				jsonMaterTestValue.append("value");
				jsonMaterTestValue.append("\":\"");
				jsonMaterTestValue.append(materPo.getMaterId());
				jsonMaterTestValue.append("\",\"text\":\"");
				jsonMaterTestValue.append(materPo.getMaterId() + ":" + materPo.getMaterName() + "--" + materPo.getUnit() + "--(品牌)"
						+ materPo.getBrands() + "--(规格)" + materPo.getSpecifications());
				jsonMaterTestValue.append("\"},");
			}
			jsonMaterTestValue = new StringBuffer(jsonMaterTestValue.substring(0, jsonMaterTestValue.length() - 1));
			jsonMaterTestValue.append("]");
		}
		return jsonMaterTestValue.toString();
	}

	public void addSupplierDistributeMaterPo(String materJson, int supId) throws GenericException {
		JSONArray array = JSONArray.fromObject(materJson);

		for (int i = 0; i < array.size(); i++) {
			JSONObject object = JSONObject.fromObject(array.get(i));
			SupplierDistributeMaterPo po = new SupplierDistributeMaterPo();
			po.setMaterId(Integer.valueOf(object.get("materId").toString()));
			po.setOrderMater(0);
			po.setSupId(supId);
			supplierDao.addSupplierDistributeMaterPo(po);
		}
	}

	public void updateSupplierDistributeMaterPo(String materJson, int supId) throws GenericException {
		JSONArray array = JSONArray.fromObject(materJson);
		for (int i = 0; i < array.size(); i++) {
			JSONObject object = JSONObject.fromObject(array.get(i));
			SupplierDistributeMaterPo po = supplierDao.getSupplierDistributeMaterPoByMaterIdSupId(Integer.valueOf(object.get("materId").toString()),
					supId).get(0);
			po.setMaterId(Integer.valueOf(object.get("materId").toString()));
			po.setOrderMater(Integer.valueOf(object.get("orderMater").toString()));
			supplierDao.addSupplierDistributeMaterPo(po);
		}
	}

	public List<SupplierDistributeMaterPo> getSupplierDistributeMaterPoBySupId(int supId) throws GenericException {
		return supplierDao.getSupplierDistributeMaterPoBySupId(supId);
	}

	public void deleteSupplierDistributeMaterPo(int materId, int supId) throws GenericException {
		supplierDao.deleteSupplierDistributeMaterPo(materId, supId);
	}

}
